package com.gitee.fsmxm.ocon.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author fsmxm
 */
public class ConstantGenerator {

    public static void main(String[] args) throws Exception {
        String fileName = "C:\\Users\\yf5\\Desktop\\enu.txt";

        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)))) {

            final String enuName = reader.readLine();
            if (enuName == null) {
                return;
            }

            final String intro = reader.readLine();

            System.out.println("/**" + intro + "\n@author fsmxm\n@see <a href=\"\"></a>*/");

            System.out.println("public final class " + enuName + " {");

            System.out.println("private " + enuName + "(){}");

            String line;
            while ((line = reader.readLine()) != null) {
                if (line.trim().isEmpty()) {
                    continue;
                }
                final String[] split = line.trim().split("\\s");

                final ArrayList<String> list = new ArrayList<>(Arrays.asList(split));

                final String varName = list.remove(0);
                final String val = list.remove(0);

                final String join = String.join(" ", list);
                if (!join.isEmpty()) {
                    System.out.println("/**" + join + "*/");
                }
                System.out.println("public static final int " + format(varName) + " = " + val + ";");
            }

            System.out.println("}");
        }
    }

    private static String format(String name) {
        final char[] chars = name.toCharArray();
        StringBuilder builder = new StringBuilder();
        for (char c : chars) {
            final int length = builder.length();
            if (length > 0 && c >= 'A' && c <= 'Z') {
                final char c1 = builder.charAt(length - 1);
                if (c1 >= 'a' && c1 <= 'z') {
                    builder.append('_');
                }
            }
            if (length > 1 && c >= 'a' && c <= 'z') {
                final char c1 = builder.charAt(length - 1);
                final char c2 = builder.charAt(length - 2);
                if (c1 >= 'A' && c1 <= 'Z' && c2 >= 'A' && c2 <= 'Z') {
                    builder.insert(length - 1, '_');
                }
            }
            builder.append(c);
        }
        return builder.toString().toUpperCase();
    }

}
